import { ElMessage, ElMessageBox, ElLoading, type ElMessageBoxOptions } from 'element-plus'
import type { LoadingInstance } from 'element-plus/es/components/loading/src/loading'
import type { MessageBoxData } from 'element-plus/es/components/message-box/src/message-box.type'
import type { MessageType } from '../../types/msg'

export class Feedback implements MessageType {
    private loadingInstance: LoadingInstance | null = null
    static instance: Feedback | null = null

    static getInstance() {
        return this.instance ?? (this.instance = new Feedback())
    }

    // 消息提示
    info(msg: string) {
        ElMessage.info(msg)
    }

    // 错误消息
    error(msg: string) {
        ElMessage.error(msg)
    }

    // 成功消息
    success(msg: string) {
        ElMessage.success(msg)
    }

    // 警告消息
    warning(msg: string) {
        ElMessage.warning(msg)
    }

    // 弹出提示
    alert(
        msg: string,
        title: string = '系统提示',
        options?: ElMessageBoxOptions
    ): Promise<MessageBoxData> {
        return ElMessageBox.alert(msg, title, options)
    }

    // 错误提示
    alertError(msg: string, title: string = '系统提示'): Promise<MessageBoxData> {
        return ElMessageBox.alert(msg, title, { type: 'error' })
    }

    // 成功提示
    alertSuccess(msg: string, title: string = '系统提示'): Promise<MessageBoxData> {
        return ElMessageBox.alert(msg, title, { type: 'success' })
    }

    // 警告提示
    alertWarning(msg: string, title: string = '系统提示'): Promise<MessageBoxData> {
        return ElMessageBox.alert(msg, title, { type: 'warning' })
    }

    // 确认窗体
    confirm(msg: string, title: string = '温馨提示', options?: ElMessageBoxOptions) {
        return ElMessageBox.confirm(msg, title, {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            ...options
        })
    }

    // 提交内容
    prompt(content: string, title: string, options?: ElMessageBoxOptions) {
        return ElMessageBox.prompt(content, title, {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            ...options
        })
    }

    // 打开全局loading
    loading(msg: string = 'loading') {
        this.loadingInstance = ElLoading.service({
            lock: true,
            text: msg
        })
    }

    // 关闭全局loading
    closeLoading() {
        this.loadingInstance?.close()
    }
}

const message = Feedback.getInstance()

export default message
